﻿using System;
using System.Runtime.CompilerServices;

namespace HideAndSeek
{
    class Opponent
    {
        private Location _myLocation;

        private Random _random;

        public Opponent(Location startingLocation)
        {
            _myLocation = startingLocation;
            _random = new Random();
        }

        public void Move()
        {
            var hidden = false;
            while (!hidden)
            {
                var locationWithDoor = _myLocation as IHasExteriorDoor;
                if (locationWithDoor != null)
                {
                    if (_random.Next(2) == 1)
                    {
                        _myLocation = locationWithDoor.DoorLocation;
                    }
                }

                int rand = _random.Next(_myLocation.Exits.Length);
                _myLocation = _myLocation.Exits[rand];
                if (_myLocation is IHidingPlace)
                {
                    hidden = true;
                }
            }
        }

        public bool Check(Location locationToCheck)
        {
            return _myLocation == locationToCheck;
        }
    }
}
